package cn.itcast.day04.demo01;

import org.omg.CORBA.TIMEOUT;

import java.util.*;

/*
@李子宁
Happy,happy everyday!
冲鸭！
2020/8/26

*/
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int len = Integer.parseInt(in.nextLine());
        int times = Integer.parseInt(in.nextLine());
        String[] s = new String[times];
        for (int i = 0; i < times; i++) {
            s[i] = in.nextLine();
        }

        int[][] nums = new int[times][2];
        Set<Character> set = new LinkedHashSet<>();
        List<char[]> clist = new ArrayList<>();
        for (int i = 0; i < times; i++) {
            String si = s[i].split(" ")[0];
            clist.add(si.toCharArray());
            for (char c : si.toCharArray()) {
                set.add(c);
            }
            nums[i][0] = Integer.parseInt(s[i].split(" ")[1]);
            nums[i][1] = Integer.parseInt(s[i].split(" ")[2]);

        }
        char[] c = new char[set.size()];
        int lenc = set.size();
        for (Character character : set) {
            c[lenc-1] = character;
            lenc--;
        }
        List<List<Character>> per = new Main().per(c);
      /*
        for (List<Character> list : per) {
            int right = 0;
            int wrong = 0;
            for (int i = 0; i < times; i++) {
                char[] chars = clist.get(i);
                for (int i1 = 0; i1 < chars.length; i1++) {
                    if (chars[i1] == list.get(i1)){
                        right ++;
                    }
                    if (list.contains(chars[i1])){
                        wrong++;
                    }
                }
                if (right< nums[i][0]||wrong<(nums[i][1]+nums[i][0])){
                    continue;
                }
            }
        }
        List<List<Character>> per = new Main().per(c);*/
        for (List<Character> list : per) {
            System.out.println(Arrays.asList(list));
        }

    }
    public List<List<Character>> per(char[] nums){
        List<List<Character>> lists = new LinkedList<>();
        temp(nums,0,lists);
        return lists;
    }

    public void temp(char[] nums, int start, List<List<Character>> lists){
        int len = nums.length;
        if (start == len-1){
            List<Character> l = new LinkedList<>();
            for (char num : nums) {
                l.add(num);
            }
            lists.add(l);
            return;
        }
        for (int i = start; i < len; i++) {
            char tmp = nums[start];
            nums[start] = nums[i];
            nums[i] = tmp;
            temp(nums,start+1,lists);
            tmp = nums[start];
            nums[start] = nums[i];
            nums[i] = tmp;
        }
    }


}
